package net.pm.core.web.user;

import java.util.List;

import javax.annotation.Resource;

import net.pm.common.toolkit.DevConstant;
import net.pm.common.toolkit.PMRetcodeWrapper;
import net.pm.core.annotation.Kaptcha;
import net.pm.core.aspect.PM_02_LoginLogAspect;
import net.pm.core.cache.SessionStorer;
import net.pm.core.entities.db.DeptPower;
import net.pm.core.entities.db.Loginlog;
import net.pm.core.entities.db.Pmdept;
import net.pm.core.entities.db.Pmuser;
import net.pm.core.entities.db.UserPower;
import net.pm.core.service.pmuser.PMUserServicer;
import net.pm.core.service.power.DeptPowerServicer;
import net.pm.core.service.power.UserPowerServicer;
import net.pm.core.web.BaseController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
@Scope("request")
public class RegisterController extends BaseController {

	@Resource(name = "PMUserServicer")
	private PMUserServicer userService;
	@Resource(name="UserPowerServicer")
	private UserPowerServicer userPowerServicer;
	@Resource(name="DeptPowerServicer")
	private DeptPowerServicer deptPowerServicer;

	/**
	 * 去到注册页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "register", method = RequestMethod.GET)
	public String login() {
		return "user/register";
	}

	/**
	 * 注册处理
	 * 
	 * @return
	 */
	@Kaptcha
	@RequestMapping(value = "register", method = RequestMethod.POST)
	public ModelAndView registerhandle(String loginname, String passwd) {
		ModelAndView mav = new ModelAndView();
		// 为作注册日志作准备
		PMRetcodeWrapper<Pmuser> retval = userService.register(loginname, passwd);
		Long retcode = retval.getRetcode();
		Pmuser user = retval.getRetobj();

		if (!retcode.equals(PMUserServicer.login_success)) {// 如果注册失败
			mav.setViewName("redirect:/login.htm");

			if (retcode.equals(PMUserServicer.login_failed_loginname_notexists)) {
				super.setSessionMessage("用户名不存在，请查正!");
			} else if (retcode.equals(PMUserServicer.login_failed_passwd_error)) {
				super.setSessionMessage("密码输入错误，请查正!");
				// 为注册日志作准备
				mav.addObject(PM_02_LoginLogAspect.LOGINRESULT_KEY,
						Loginlog.logResult_passwd_error);
			} else if (retcode
					.equals(PMUserServicer.login_failed_account_disable)) {
				super.setSessionMessage("对不起，您的帐户己被管理员禁用，请联系管理员!");
				// 为注册日志作准备
				mav.addObject(PM_02_LoginLogAspect.LOGINRESULT_KEY,
						Loginlog.logResult_account_disable);
			}
			// 为注册日志作准备
			mav.addObject(PM_02_LoginLogAspect.CLIENTIP_KEY, super
					.obtainRequest().getRemoteAddr());
			mav.addObject(PM_02_LoginLogAspect.OWER_KEY, user);
			return mav;
		}

		// 用户注册成功，存入session
		Pmdept dept = user.getPmdept();
		//用户
		super.obtainSession().setAttribute(DevConstant.SESSION_CURRENT_USER,
				user);
		//部门
		super.obtainSession().setAttribute(DevConstant.SESSION_CURRENT_DEPT,
				dept);
		
		//用户权限
		super.obtainSession().setAttribute(
				DevConstant.SESSION_CURRENT_USER_POWER, userPowerServicer.findUserPowerCodesByUserId(user.getId()));
		
		
		//部门权限
		super.obtainSession().setAttribute(
				DevConstant.SESSION_CURRENT_DEPT_POWER,deptPowerServicer.findDeptPowerCodesByDeptId(dept.getId()));

		// 存入session缓存库
		SessionStorer.put(dept.getId(), user.getId(), super.obtainSession());

		super.setSessionMessage("用户注册成功！");
		mav.setViewName("redirect:/home.htm");

		// 为登录日志作准备
		mav.addObject(PM_02_LoginLogAspect.LOGINRESULT_KEY,
				Loginlog.logResult_login_success);
		mav.addObject(PM_02_LoginLogAspect.CLIENTIP_KEY, super.obtainRequest()
				.getRemoteAddr());
		mav.addObject(PM_02_LoginLogAspect.OWER_KEY, user);
		return mav;

	}

	
	/**
	 * 是否包含,拥有该权限
	 * 
	 * @param id
	 * @param userpowers
	 * @return
	 */
	public static Boolean isContainsUserPower(Integer id,
			List<UserPower> userpowers) {
		for (UserPower userpower : userpowers) {
			if (userpower.getPmpower().getId().equals(id)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 是否包含,拥有该权限
	 * 
	 * @param id
	 * @param deptusers
	 * @return
	 */
	public static Boolean isContainsDeptPower(Integer id,
			List<DeptPower> deptpowers) {
		for (DeptPower deptpower : deptpowers) {
			if (deptpower.getPmpower().getId().equals(id)) {
				return true;
			}
		}
		return false;
	}
}
